﻿Public Class frmClientCPT
    Public pCd_cli As Integer
    Public pNom_cli As String

    Private szSQL As String
    Private rs As ADODB.Recordset
    Private m_SortingColumn As ColumnHeader
    Private m_SortingIndex As Integer = 0

    Private Sub frmClientCPT_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Me.Text = "COMPTE CLIENT : " & pCd_cli & " - " & pNom_cli
        Me.BackColor = getSocieteColor()
        Call InitComptes(0)

    End Sub

    Private Sub InitComptes(ByVal ix As Integer)

        Dim nb As Integer = 0
        Dim debit As Decimal
        Dim credit As Decimal
        Dim solde As Decimal

        ' effacer la listview
        LV_COMPTE.Items.Clear()
        LV_COMPTE.ListViewItemSorter = Nothing

        ' lire tous le ompte client
        szSQL = "CALL spGET_CLIENT_COMPTE('" & gSocieteCode & "'," & pCd_cli & ")"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)

        Do While Not rs.EOF
            LV_COMPTE.Items.Add(rs.Fields("date").Value)
            LV_COMPTE.Items(nb).SubItems.Add(rs.Fields("noligne").Value)
            LV_COMPTE.Items(nb).SubItems.Add(rs.Fields("libelle").Value)
            LV_COMPTE.Items(nb).SubItems.Add(Zero2Space(rs.Fields("debit").Value))
            LV_COMPTE.Items(nb).SubItems.Add(Zero2Space(rs.Fields("credit").Value))
            LV_COMPTE.Items(nb).SubItems.Add((rs.Fields("solde").Value))
            LV_COMPTE.Items(nb).SubItems.Add("")
            If rs.Fields("solde").Value > 0 Then
                LV_COMPTE.Items(nb).ForeColor = Color.Red
            Else
                LV_COMPTE.Items(nb).ForeColor = Color.Green
            End If
            debit += CDec(rs.Fields("debit").Value)
            credit += CDec(rs.Fields("credit").Value)
            rs.MoveNext()
            nb = nb + 1
        Loop
        rs.Close()

        TE_DEBIT.Text = debit.ToString("C")
        TE_CREDIT.Text = credit.ToString("C")

        solde = credit - debit
        TE_SOLDE.Text = solde.ToString("C")
        If solde < 0 Then
            TE_SOLDE.ForeColor = Color.Red
        Else
            TE_SOLDE.ForeColor = Color.Green
        End If

        ' restaurer l'ancienne ordre de tri de la listview
        If m_SortingIndex > 0 Then
            m_SortingColumn.Text = m_SortingColumn.Text.Replace("> ", "")
            SortListView(LV_COMPTE, m_SortingColumn, m_SortingIndex)
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_COMPTE)

    End Sub

    Private Sub LV_COMPTE_ColumnClick(sender As Object, e As System.Windows.Forms.ColumnClickEventArgs) Handles LV_COMPTE.ColumnClick
        SortListView(LV_COMPTE, m_SortingColumn, e.Column)
        m_SortingIndex = e.Column
    End Sub

    Private Sub LV_COMPTE_DoubleClick(sender As Object, e As System.EventArgs) Handles LV_COMPTE.DoubleClick
        Dim szNoLigne As String

        If LV_COMPTE.SelectedItems.Count > 0 Then
            szNoLigne = LV_COMPTE.SelectedItems(0).SubItems(1).Text
            ' est-ce une facture ?
            If szNoLigne.Substring(0, 2) = "FA" Then
                frmReglerFacture.pCd_cli = pCd_cli
                frmReglerFacture.pNo_fact = szNoLigne.Substring(3, 6)
                frmReglerFacture.pTotalTTC = LV_COMPTE.SelectedItems(0).SubItems(3).Text
                frmReglerFacture.ShowDialog()
                ' réaffiche la viewlist
                InitComptes(LV_COMPTE.SelectedItems(0).Index)
            Else
                frmFacturesReglees.pNoVersement = szNoLigne
                frmFacturesReglees.pMontant = LV_COMPTE.SelectedItems(0).SubItems(4).Text
                frmFacturesReglees.ShowDialog()
            End If
        End If

    End Sub

End Class